<template>
  <page-header-wrapper>
    <a-card :bordered="false">

      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleImport">导入</a-button>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
      >
        <span slot="created_at" slot-scope="text">
          <span>{{ text | dayjs }}</span>
        </span>


        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleView(record)">查看</a>
              <a-divider type="vertical" />
            <a @click="handleDel(record)" >删除</a>
          </template>
        </span>
      </s-table>

      <import-form
        ref="importModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
      <a-modal v-model="viewShow">
        <a-descriptions title="查看详细">
          <a-descriptions-item label="月份">
            {{viewData.date_month}}
          </a-descriptions-item>
          <a-descriptions-item label="姓名">
           {{viewData.name}}
          </a-descriptions-item>
          <a-descriptions-item label="工作天数">
           {{viewData.work_days}}
          </a-descriptions-item>
           <a-descriptions-item label="实际天数">
            {{viewData.real_days}}
          </a-descriptions-item>
           <a-descriptions-item label="基本工资">
            {{viewData.base_pay}}
          </a-descriptions-item>
           <a-descriptions-item label="岗位工资">
           {{viewData.post_pay}}
          </a-descriptions-item>
           <a-descriptions-item label="出勤工资">
           {{viewData.work_pay}}
          </a-descriptions-item>
           <a-descriptions-item label="工龄">
           {{viewData.work_age}}
          </a-descriptions-item>
           <a-descriptions-item label="应得工资">
             {{viewData.due_pay}}
          </a-descriptions-item>
           <a-descriptions-item label="优秀">
            {{viewData.excellent}}
          </a-descriptions-item>
           <a-descriptions-item label="其他">
            {{viewData.other_pay}}
          </a-descriptions-item>
           <a-descriptions-item label="保险">
           {{viewData.insurance}}
          </a-descriptions-item>
           <a-descriptions-item label="绩效工资">
             {{viewData.merits}}
          </a-descriptions-item>
           <a-descriptions-item label="工会经费">
             {{viewData.union_fee}}
          </a-descriptions-item>
           <a-descriptions-item label="实得工资">
            {{viewData.real_pay}}
          </a-descriptions-item>
            <a-descriptions-item label="备注">
            {{viewData.remark}}
          </a-descriptions-item>
        </a-descriptions>
      </a-modal>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { getList, imports, deletes } from '@/api/salary'

import ImportForm from './modules/ImportForm'

const columns = [
    {
    title: '月份',
    align: 'center',
    dataIndex: 'date_month',
  },
  {
    title: '名称',
    dataIndex: 'name'
  },
   {
    title: '工作天数',
    dataIndex: 'work_days'
  },
  {
    title: '实际天数',
    dataIndex: 'real_days'
  },
  {
    title: '实得工资',
    dataIndex: 'real_pay'
  },
  
  {
    title: '创建时间',
    align: 'center',
    dataIndex: 'created_at',
    scopedSlots: { customRender: 'created_at' }
  },

  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'Dept',
  components: {
    STable,
    Ellipsis,
    ImportForm,
  },
  data () {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      viewData:{},
      viewShow:false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getList(requestParameters)
          .then(res => {
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    handleImport () {
      this.mdl = {
        disabled: false
      }
      this.visible = true
    },
    handleView(record){
        this.viewShow = true;
        this.viewData = record;
    },
    handleDel(record){
       this.confirmLoading = true
        deletes(record.id).then(ret => {
          this.visible = false
          this.confirmLoading = false
          // 刷新表格
          this.$refs.table.refresh()
          this.$message.info('删除成功')
        })
    },
    handleOk (values) {
      const form = this.$refs.importModal
      this.confirmLoading = true
      const formatData = new FormData()
      formatData.append('file', values.file)
      formatData.append('date', values.date)
        imports(formatData).then(res => {
          this.visible = false
          this.confirmLoading = false
          // 重置表单数据
          form.resetFields()
         if (res.success) {
           // 刷新表格
           this.$refs.table.refresh()
           this.$message.info(res.msg)
         } else {
           this.$message.info(res.msg)
         }
        })
    },
    handleCancel () {
      this.visible = false
      const form = this.$refs.importModal
      form.resetFields() // 清理表单数据（可不做）
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
